#ifndef _TCPSERVERCONNECTION_HPP_
#define _TCPSERVERCONNECTION_HPP_

#include "jhdefs.hpp"
#include "net/StreamSocket.hpp"
#include "thread/runnable.hpp"
#include "thread/errorhandler.hpp"
using namespace JHCPP::thread;
#include "utils/shareptr.hpp"
using JHCPP::utils::TSharedPtr;

NET_NAMESPACE_BEGIN

//
// CTCPServerConnection
//
class CTCPServerConnection : public CRunnable
{
public:
	CTCPServerConnection(const CStreamSocket& socket);
	virtual ~CTCPServerConnection();

protected:
	CStreamSocket& socket();
	void start();

private:
	CTCPServerConnection();
	CTCPServerConnection(const CTCPServerConnection&);
	CTCPServerConnection& operator = (const CTCPServerConnection&);

	CStreamSocket m_socket;

	friend class CTCPServerDispatcher;
};


//
// CTCPServerConnectionFactory
//
class CTCPServerConnectionFactory
{
public:
	typedef TSharedPtr<CTCPServerConnectionFactory> Ptr;
	virtual ~CTCPServerConnectionFactory();

	virtual CTCPServerConnection* createConnection(const CStreamSocket& socket) = 0;

protected:
	CTCPServerConnectionFactory();

private:
	CTCPServerConnectionFactory(const CTCPServerConnectionFactory&);
	CTCPServerConnectionFactory& operator = (const CTCPServerConnectionFactory&);
};


//
// TTCPServerConnectionFactoryImpl
//
template <class T>
class TTCPServerConnectionFactoryImpl : public CTCPServerConnectionFactory
{
public:
	TTCPServerConnectionFactoryImpl()
	{
	}

	~TTCPServerConnectionFactoryImpl()
	{
	}

	CTCPServerConnection* createConnection(const CStreamSocket& socket)
	{
		return new T(socket);
	}
};


NET_NAMESPACE_END

#endif
